## Uncertainty and Conflict Termination Preferences ##
## Data preparation ##

# load packages
source("code/packages.r")

# load data
lapop16_df <- haven::read_dta("data/lapop2016.dta")
lapop13_df <- haven::read_dta("data/lapop2013.dta")

## data preparation ----------------------------------------------------

# lists
conflict_list <- c("5665", "18001", "23466", "52835", "76109", "76275", "86568") #Municipalities in Espada de Honor (Matanock2018, Tellez2018)

conc_list <- c('colconce4', 'colpact1', 'colpact11a', 'colpact11', 'colpact14') #Varibles concerning concessions to FARC combatants
conc_list_scaled <- c('scaled_colconce4', 'scaled_colpact1', 'scaled_colpact11a', 'scaled_colpact11', 'scaled_colpact14') # Scaled varibles concerning concessions to FARC combatants

no_farc_conc_list <- c('colpact8', 'colpact9', 'colpact10', 'colpact12', 'colpact13') #Varibles concerning non-FARC combatant concessions  
no_farc_conc_list_scaled <- c('scaled_colpact8', 'scaled_colpact9', 'scaled_colpact10', 'scaled_colpact12', 'scaled_colpact13') #Scaled varibles concerning non-FARC combatant concessions  

# pca for latent concessions variable
conce_scale_fit <-  psych::principal(dplyr::select(lapop16_df, all_of(conc_list)), nfactors = 2, rotate = "varimax")
no_farc_conce_scale_fit <- psych::principal(dplyr::select(lapop16_df, all_of(no_farc_conc_list)), nfactors = 2, rotate = "varimax")

# preparing variables of interest
survey_df <- lapop16_df %>% 
  dplyr::mutate(neg_farc = dplyr::case_when(lapop16_df$colpaz1a == 1 ~ 1, 
                                            lapop16_df$colpaz1a %in% c(2,3) ~ 0,
                                            T ~ NA_real_), #negotiation with FARC
                neg_bacrim = dplyr::case_when(lapop16_df$colpaz1c == 1 ~ 1, 
                                              lapop16_df$colpaz1c %in% c(2,3) ~ 0,
                                              T ~ NA_real_), #negotiation with BACRIM
                #demographics and covariate adjustment
                urban = ifelse(lapop16_df$ur == 1, 1, 0), #urban
                votesantos = dplyr::case_when(lapop16_df$vb3n == 804 ~ 1,
                                              T ~ 0), #voted for Juan Manuel Santos
                estrato = dplyr::case_when(lapop16_df$mov1n == 1 ~ 5,
                                           lapop16_df$mov1n == 2 ~ 4,
                                           lapop16_df$mov1n == 3 ~ 3,
                                           lapop16_df$mov1n == 4 ~ 2,
                                           lapop16_df$mov1n == 5 ~ 1), #income group
                age = lapop16_df$q2, #age
                edu = lapop16_df$ed, #education
                female = ifelse(lapop16_df$q1 == 2, 1, 0), #female
                prov_label = labelled::to_factor(lapop16_df$prov), #province text
                conflict_zone = ifelse(lapop16_df$upm %in% conflict_list, 1, 0), #municipality is conflict-zone
                reg_treat = ifelse(lapop16_df$prov %in% c(815, 818, 820, 841, 850), 0, 1), #regions with control and treated units
                relative_vict = ifelse(lapop16_df$colwc4gen %in% c(1,2,3), 1, 0), #proximity to conflict (relative as victim)
                relative_farc_vict = dplyr::case_when(lapop16_df$colwc4a == 1  ~ 1,
                                                      T ~ 0), #proximity to conflict (relative as FARC victim)
                victim_ruv = ifelse(lapop16_df$collt5 == 1, 1, 0), #respondent is a registered victim
                #event
                d = ifelse(lapop16_df$fecha <= "2016-10-02", 0, 1), #event D
                date_zero = as.numeric(lapop16_df$fecha - lubridate::as_date("2016-10-02")), #dates from reference
                #support for Santos FARC agreement
                scaled_support_agreement = scale(lapop16_df$colpropaz1b),
                #scale concessions
                scaled_colconce4 = scale(lapop16_df$colconce4),
                scaled_colpact1 = scale(lapop16_df$colpact1),
                scaled_colpact11a = scale(lapop16_df$colpact11a),
                scaled_colpact11 = scale(lapop16_df$colpact11),
                scaled_colpact14 = scale(lapop16_df$colpact14),
                #concessions
                conce_agg = rowMeans(lapop16_df[conc_list], na.rm = T), #aggregate concessions
                concession_latent = predict(conce_scale_fit, dplyr::select(lapop16_df, dplyr::all_of(conc_list)))[,1], #latent concessions
                #scale non-FARC concessions
                scaled_colpact8 = scale(lapop16_df$colpact8),
                scaled_colpact9 = scale(lapop16_df$colpact9),
                scaled_colpact10 = scale(lapop16_df$colpact10),
                scaled_colpact12 = scale(lapop16_df$colpact12),
                scaled_colpact13 = scale(lapop16_df$colpact13),
                #concessions
                no_farc_conce_agg = rowMeans(lapop16_df[no_farc_conc_list], na.rm = T), #aggregate concessions
                no_farc_concession_latent = predict(no_farc_conce_scale_fit, dplyr::select(lapop16_df, dplyr::all_of(no_farc_conc_list)))[,1], #latent concessions
                #belief in FARC members
                farc_demobilize = dplyr::case_when(colpropaz2a == 1 ~ 4, #Very likely
                                                   colpropaz2a == 2 ~ 3, #Likely
                                                   colpropaz2a == 3 ~ 2, #Somewhat likely
                                                   colpropaz2a == 4 ~ 1, #Not likely at all
                                                   T ~ NA_real_),
                farc_stop_drug_traffic = dplyr::case_when(colpropaz2b == 1 ~ 4, #Very likely
                                                          colpropaz2b == 2 ~ 3, #Likely
                                                          colpropaz2b == 3 ~ 2, #Somewhat likely
                                                          colpropaz2b == 4 ~ 1, #Not likely at all
                                                          T ~ NA_real_),
                #bandwiths for robustness
                bw15 = ifelse(dplyr::between(lapop16_df$fecha,lubridate::as_date("2016-09-16"),lubridate::as_date("2016-10-16")), 1, 0),
                bw7 = ifelse(dplyr::between(lapop16_df$fecha,lubridate::as_date("2016-09-25"),lubridate::as_date("2016-10-09")), 1, 0),
                #most important problem
                problem = haven::as_factor(lapop16_df$a4),
                #placebo events
                p15 = ifelse(lapop16_df$fecha > (lubridate::ymd("2016-10-02") - lubridate::days(15)), 1, 0),
                p30 = ifelse(lapop16_df$fecha > (lubridate::ymd("2016-10-02") - lubridate::days(30)), 1, 0),
                p45 = ifelse(lapop16_df$fecha > (lubridate::ymd("2016-10-02") - lubridate::days(45)), 1, 0)
  )

survey_df$conce_agg_scale <- rowMeans(survey_df[conc_list_scaled], na.rm = T)
survey_df$no_farc_conce_agg_scale <- rowMeans(survey_df[no_farc_conc_list_scaled], na.rm = T)

# lapop 2013 preparation --------------------------------
survey13_df <- lapop13_df %>%
  dplyr::mutate(neg_farc = dplyr::case_when(lapop13_df$colpaz1a == 1 ~ 1, 
                                            lapop13_df$colpaz1a %in% c(2,3) ~ 0,
                                            T ~ NA_real_), #negotiation with FARC
                d = ifelse(lapop13_df$fecha3 <= "2013-10-02", 0, 1),
                relative_farc_vict = dplyr::case_when(lapop13_df$colwc4a == 1  ~ 1,
                                                      T ~ 0), #proximity to conflict (relative as FARC victim)
                victim_ruv = ifelse(lapop13_df$collt5 == 1, 1, 0), #respondent is a registered victim
                age = lapop13_df$q2, #age
                edu = lapop13_df$ed, #education
                female = ifelse(lapop13_df$q1 == 2, 1, 0),
                conflict_zone = ifelse(lapop13_df$upm %in% conflict_list, 1, 0), #municipality is conflict-zone
                estrato = dplyr::case_when(lapop13_df$mov1 == 1 ~ 5,
                                           lapop13_df$mov1 == 2 ~ 4,
                                           lapop13_df$mov1 == 3 ~ 3,
                                           lapop13_df$mov1 == 4 ~ 2,
                                           lapop13_df$mov1 == 5 ~ 1), #income group
                urban = ifelse(lapop13_df$ur == 1, 1, 0), #urban
                votesantos = dplyr::case_when(lapop13_df$vb20 == 2 ~ 1,
                                              T ~ 0) #next election vote for Juan Manuel Santos
  )


#save prepared df
save(survey_df, file = "data/survey_df.RData")
save(survey13_df, file = "data/survey13_df.RData")
